<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>123</div>
    <div>
        <p>1</p>
        <p>2</p>
        <p>3</p>
    </div>
    <script>
        //1. 重写数组的every方法(有一个为false就返回false)
        Array.prototype.myEvery = function (cb) {
            for (var i = 0; i < this.length; i++) {
                if (!cb(this[i], i, this)) return false
            }
            return true
        }

        //2. 重写数组的some方法(有一个为true就返回true)
        Array.prototype.mySome = function (cb) {
            for (var i = 0; i < this.length; i++) {
                if (cb(this[i], i, this)) return true
            }
            return false
        }

        // 3.重写数组的map方法
        Array.prototype.myMap = function (cb) {
            var res = []
            for (var i = 0; i < this.length; i++) {
                res.push(cb(this[i], i, this))
            }
            return res
        }


        // 4.重写数组的forEach方法
        Array.prototype.myForEach = function (cb) {
            for (var i = 0; i < this.length; i++) {
                cb(this[i], i, this)
            }
        }

        // 5.重写数组的filter方法
        Array.prototype.myFilter = function (cb) {
            var res = []
            for (var i = 0; i < this.length; i++) {
                if (cb(this[i], i, this)) res.push(this[i])
            }
            return res
        }

        // 6.重写数组的find方法
        Array.prototype.myFind = function (cb) {
            for (var i = 0; i < this.length; i++) {
                if (cb(this[i], i, this)) return this[i]
            }
        }

        // 7.重写数组的findIndex方法
        Array.prototype.myFindIndex = function (cb) {
            for (var i = 0; i < this.length; i++) {
                if (cb(this[i], i, this)) return i
            }
        }


        // 8.重写数组的flat方法(拍平数组)
        Array.prototype.myFlat = function (num = 1) {
            var arr = this
            var i = 0
            while (arr.some(item => Array.isArray(item))) {
                arr = [].concat(...arr)
                i++
                if (i >= num) break
            }
            return arr
        }

        var arr = [true, false, false, false, false]
        console.log(arr.mySome(item => item));
        console.log(arr.myEvery(item => item));
    </script>
</body>

</html>